package cn.edu.yzu.scholarship.model;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.edu.yzu.scholarship.constant.C;

import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class Freeze extends Model<Freeze> {

	private static final long serialVersionUID = -3876131726842295933L;
	
	public static final Freeze dao = new Freeze();

	public static void insertOne(String xh, String startdate, String enddate, String reason,String scholarshipId) {
		if(C.PARAM_ALL.equals(scholarshipId)) {//全部奖学金
			Db.update(SqlKit.sql("freeze.deleteOne"), xh);
			for(Record record : Db.find("select id from scholarship")) {
				Db.update(SqlKit.sql("freeze.insertOne"), xh, startdate, enddate, reason, record.get("id"));
			}
		} else 
			Db.update(SqlKit.sql("freeze.deleteOneByXhAndScholarshipId"), xh, scholarshipId);
			Db.update(SqlKit.sql("freeze.insertOne"), xh, startdate, enddate, reason, scholarshipId);
		
		Db.update(SqlKit.sql("freeze.recordInsert"), xh, startdate, enddate, reason, scholarshipId);
	}
	
	public static List<Record> findFreezingList(String scholarshipId) {
		return Db.find(
				SqlKit.sql("freeze.findFreezingList") + 
				(StringUtils.isBlank(scholarshipId) ? ""
						: " and scholarshipId='" + scholarshipId + "'")
							);
	}
	
	public static void deleteOne(String xh,String scholarshipId, String reason) {
		if(C.PARAM_ALL.equals(scholarshipId)) //全部
			Db.update(SqlKit.sql("freeze.deleteOne"),xh);
		else 
			Db.update(SqlKit.sql("freeze.deleteOneByXhAndScholarshipId"), xh, scholarshipId);
		
		Db.update(SqlKit.sql("freeze.recordDelete"), xh, scholarshipId, reason);
	}
}
